
<template>
<!-- 承运商管理 -->
  <div>
    <section>
      <el-form
        ref="form"
        :model="queryForm"
        label-width="71px"
        :inline="true"
        class="queryForm adjustCondition"
      >
        <el-form-item label="关键字:" label-width="61px">
          <el-input
            v-model="queryForm.name"
            size="small"
            placeholder="输入承运商名称/证件号/手机号查询"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <div class="but-container">
            <el-button
              type="primary"
              size="small"
              class="search-btn"
              @click="
                getList(
                  page,
                  3,
                  orderBy.name,
                  orderBy.method,
                  queryForm.name
                )
              "
              >查询</el-button
            >
            <el-button size="small" @click="reset">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </section>
    <div>
      <!-- <span style="font-size: 15px; font-weight: 400; color: #999999"
        >总共{{this.page.total}}家承运商</span
      > -->
      <el-button
        type="primary"
        size="small"
        @click="add"
        >新增</el-button
      >
<!--      <el-button size="small" >导入</el-button>-->
<!--      <el-button type="default" size="small"-->
<!--                 @click="exportExcel(page, 3,orderBy.name,orderBy.method,queryForm.name,queryForm.checkedState,queryForm.areaCode,queryForm.productCategoryId)">导出</el-button>-->
      <section class="right-but" style="display:flex">
          <el-button type="text" size="small"
                     @click="exportExcel(page, 3,orderBy.name,orderBy.method,queryForm.name,queryForm.checkedState,queryForm.areaCode,queryForm.productCategoryId)">
              <img src="~@/assets/img/export.png" alt="">
          </el-button>
        <el-button type="text" style="margin-right:-13px" @click="getList(page, 3,orderBy.name,orderBy.method,queryForm.name,queryForm.checkedState)">
          <img style="width:44px;height:36px;" src="~@/assets/img/refresh.png" >
        </el-button>
        <PopoverBut :checkedProps="checkedProps"  :options="options" @getCheckeds="handlerCloumn"></PopoverBut>
      </section>
    </div>
    <!-- <section class="right-but" style="display:flex">
      <span style="margin-right:10px;cursor:pointer;" @click="getList(page, 3,orderBy.name,orderBy.method,queryForm.name,queryForm.checkedState)">
        <img style="width:44px;height:36px;margin-top:-15px" src="~@/assets/img/shuaxin.png" >
      </span>
      <PopoverBut :checkedProps="checkedProps" style="margin-top:-16px;margin-top: -27px;margin-left: -12px;" :options="options" @getCheckeds="handlerCloumn"></PopoverBut>

    </section> -->
    <el-table
      size="mini"
      v-loading="loading"
      :data="tableData"
      style="width: 100%; margin-top: 10px"
      border
      :header-cell-style="{
        'background-color': '#eeeeee !important',
      }"
      :row-class-name="tableRowClassName"
    >
      <el-table-column type="index" label="序号" width="70" align="center">
        <template slot-scope="scope">
          <span>{{
            (page.pageNo - 1) * page.pageSize + scope.$index + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="names" label="名称" min-width="210px">
      </el-table-column>
      <el-table-column prop="types" label="类型" min-width="100">
        <template slot-scope="scope">
          {{$dictUtils.getDictLabel("companyType", scope.row.types, "")}}
        </template>
      </el-table-column>
      <el-table-column prop="idCard" label="承运商证件号码" min-width="220px">
          <template slot-scope="scope">
              {{scope.row.idCard | noValue}}
          </template>
      </el-table-column>
      <el-table-column prop="contactName" label="联系人" min-width="110">
      </el-table-column>
      <el-table-column prop="contactPhone" label="联系电话" min-width="150px">
      </el-table-column>

      <el-table-column prop="state" label="状态" width="110">
        <!-- <template slot-scope="scope">
          <el-switch
            v-model="scope.row.state"
            :inactive-value="0"
            :active-value="1"
            @change="editState($event, scope.row)"
          >
          </el-switch>
        </template> -->
        <template slot-scope="scope">
          <section class="state normal" v-if="scope.row.state===1">
            <span></span>{{$dictUtils.getDictLabel("state", scope.row.state, "")}}
          </section>
          <section class="state empty" v-else>
            <span></span>{{$dictUtils.getDictLabel("state", scope.row.state, "")}}
          </section>
        </template>
      </el-table-column>
      <span v-for="(item, index) in checkedProps" :key="index">
        <el-table-column prop="createDate" :label="item"  min-width="200px" v-if="item==='创建时间'">
            <template slot-scope="scope">
                {{scope.row.createDate | noValue}}
            </template>
        </el-table-column>
          <el-table-column prop="createBy.loginName" min-width="110px" :label="item" v-if="item==='创建人'">
              <template slot-scope="scope">
                  <span v-if="scope.row.createBy">
                      <span v-if="scope.row.createBy.loginName">
                          {{scope.row.createBy.loginName | noValue}}
                      </span>
                      <span v-else>--</span>
                  </span>
                  <span v-else>--</span>
              </template>
          </el-table-column>
          <el-table-column prop="updateDate" :label="item"  min-width="200px" v-if="item==='上次操作时间'">
              <template slot-scope="scope">
                  {{scope.row.updateDate | noValue}}
              </template>
          </el-table-column>
          <el-table-column prop="updateBy.loginName" min-width="120px" :label="item" v-if="item==='上次操作人'">
              <template slot-scope="scope">
                  <span v-if="scope.row.updateBy">
                      <span v-if="scope.row.updateBy.loginName">
                          {{scope.row.updateBy.loginName | noValue}}
                      </span>
                      <span v-else>--</span>
                  </span>
                  <span v-else>--</span>
              </template>
          </el-table-column>
      </span>

      <el-table-column prop="address" label="操作" width="196px">
        <template slot-scope="scope">
            <template v-for="item in $dictUtils.getDictList('state')">
                <el-link type="primary" v-if="item.value !=='-1'&& item.value!==(scope.row.state+'')" :underline="false" @click="isRepeat(item.value,scope.row)"  :disabled="(scope.row.state===-1&& item.value==='0')||item.value==scope.row.state"
                >{{item.label}}
                </el-link>
            </template>
          <el-link type="primary" :underline="false" @click="edit(scope.row)"
            >修改</el-link
          >
          <el-link
            type="primary"
            :underline="false"
            @click="isCheck(scope.row)"
            >删除</el-link
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-size="page.pageSize"
      :current-page="page.pageNo"
      layout="total,sizes,prev, pager, next, jumper"
      :total="page.total"
    >
    </el-pagination>
    <EditForm
      ref="EditForm"
      @refreshDataList="getList(page, 3, orderBy.name,orderBy.method)"
    ></EditForm>
  </div>
</template>
<script>
import { getList, delCrm, editState, isCheckPlan,getById} from '@/api/market/crm.js'
import EditForm from './editForm'
import PopoverBut from '@/components/button/popoverBut'
const options1 = []
const cityOptions = ['上海', '北京']
export default {
  components: {
    EditForm,
    PopoverBut
  },

  data () {
    return {
      msg: '',
      checkedProps: options1,
      ShowCloum: { createDate: '' },
      options: [{
        label: '创建人',
        key: 'createBy.loginName'
      }, {
        label: '创建时间',
        key: 'createDate'
      },{
        label: '上次操作人',
        key: 'updateBy.loginName'
      }, {
        label: '上次操作时间',
        key: 'updateDate'
      }],
      tableData: [],
      orderBy: {
        name: 'update_date',
        method: 'desc'
      },
      loading: false,
      page: {
        pageSize: 10,
        pageNo: 1,
        total: 0
      },
      props: 3,
      queryForm: {
        checkedCities: 0
      },
      checkedCities: [],
      cities: cityOptions
    }
  },
  methods: {
    // 校验：该客户信息是否已被使用
    isCheck (row) {
      isCheckPlan({
        id: row.id
      }).then((res) => {
        if (JSON.stringify(res.data.result)!=="[]") {
          this.$notify.error({
            title: '错误',
            message: '该承运商被发运计划引用，不支持删除！'
          })
        } else {
          this.delCrm(row.id)
        }
      });
    },
    // 停用确认
    isRepeat (event, row) {
      if (row.state === 1) {
        console.log(row)
        this.msg = '停用后，该承运商将不可被使用，确认停用吗？';
        this.editState(event, row.id)

      } else {
        this.msg = '您是否要' + this.$dictUtils.getDictLabel("state", event, "") + '?'
        this.editState(event, row.id)
      }
    },
    exportExcel (params, props, orderName, orderByAORC, queryNames, queryState, areaCode,productCategoryId) {
      this.$http({
        method: 'get',
        url: '/basis/sales/company/export',
        params: {
          pageSize: params.pageSize,
          pageNo: params.pageNo,
          props: props,
          orderBy: orderName,
          orderByAORC: orderByAORC,
          names: queryNames,
          state: queryState,
          countyCode: areaCode,
          productCategoryId: productCategoryId
        },
        responseType: 'blob'
      }).then(response => {
        if (!response) {
          return
        }
        let link = document.createElement('a')
        link.href = window.URL.createObjectURL(new Blob([response.data]))
        link.target = '_blank'
        let filename = response.headers['content-disposition']
        link.download = decodeURI(filename)
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        // eslint-disable-next-line handle-callback-err
      }).catch((error) => {

      })
    },
    // 获取列表带分页
    getList (params, props, orderName, orderByAORC, queryNames, queryState) {
      this.loading = true
      getList({
        pageSize: params.pageSize,
        pageNo: params.pageNo,
        props: props,
        orderBy: orderName,
        orderByAORC: orderByAORC,
        names: queryNames,
        state: queryState
      }).then((res) => {
        if (res.data.code === '200') {
          this.tableData = res.data.result.list
          this.page.total = res.data.result.count
        }
        this.loading = false
      })
    },
    // 删除客户
    delCrm (ids) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((res) => {
        delCrm ({
          ids: ids
        }).then((res) => {
          if (res.data.code === '200') {
            this.$message({
              type: 'success',
              message: '已删除!'
            })
            this.getList(this.page,3,this.orderBy.name,this.orderBy.method)
          }
        })
      });
    },
    // 添加
    add () {
      this.$refs.EditForm.init('add')
    },
    // 编辑
    edit (row) {
      getById({id: row.id}).then((res) => {
        if (res.data.code === '200') {
          let editForm = {}
          editForm.types = res.data.result.types
          editForm.id = res.data.result.id
          editForm.addressId = res.data.result.addressId
          editForm.names = res.data.result.names
          editForm.codes = res.data.result.codes
          editForm.idCard = res.data.result.idCard
          editForm.contactName = res.data.result.contactName
          editForm.contactPhone = res.data.result.contactPhone
          editForm.detailAddress = res.data.result.detailAddress
          editForm.countyCode = res.data.result.countyCode
          if (editForm.countyCode === 'undefined') {
            editForm.countyCode = ''
          }
          this.$refs.EditForm.init('edit', editForm)
        } else {
          this.$message({
            type: 'error',
            message: res.data.message
          })
        }
      })
    },
    // 改变客户状态
    editState (state, id) {
      this.$confirm(this.msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((res) => {
        editState({id: id, state: state}).then(res => {
          if (res.data.code === '200') {
            this.getList(this.page, 3, this.orderBy.name, this.orderBy.method)
            this.$message({
              type: 'success',
              message: '状态修改成功'
            })
          }
        })
      })
    },
    // 是否显示某列数据
    handlerCloumn (e) {
      this.checkedProps = e
      // console.log(e)
    },
    // 重置查询条件
    reset () {
      this.queryForm = {}
    },
    // 排序
    sortthiscolumn (a) {
      this.orderBy.name = a.prop
      this.page.pageNo = 1
      if (a.order === 'ascending') {
        this.orderBy.method = 'asc'
      } else {
        this.orderBy.method = 'desc'
      }
      this.getList(this.page, 3, this.orderBy.name, this.orderBy.method)
    },
    handleSizeChange (e) {
      this.page.pageSize = e
      this.getList(this.page, 3, this.orderBy.name, this.orderBy.method)
    },
    handleCurrentChange (e) {
      this.page.pageNo = e
      this.getList(this.page, 3, this.orderBy.name, this.orderBy.method)
      console.log(e)
    },
    tableRowClassName ({ row, rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'warning-row'
      } else {
        return 'success-row'
      }
    }
  },
  mounted () {
    // this.tableData = [{names: '1'}]
    this.getList(this.page, 3, this.orderBy.name, this.orderBy.method)
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-input--small .el-input__inner {
  height: 36px;
  line-height: 32px;
  width: 270px;
}
.queryForm {
  position: relative;
  .but-container {
    top: 10px;
    margin-left: 40px;
  }
}
.el-table .success-row {
  background: #f0f9eb;
}
::v-deep .el-table .warning-row {
  background: #f7f7f7;
}
</style>
